﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

/// <summary>
/// 
/// </summary>
namespace HRM.Web.master
{
    public partial class AddUsers : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                FillRoles();
                GetDataByUserID();
            }
        }

        /// <summary>
        /// Fills the roles.
        /// </summary>
        private void FillRoles()
        {
            BLL.Role objGetRoles = new BLL.Role();
            ddlRoles.DataSource = objGetRoles.GetAllRoles();
            ddlRoles.DataTextField = "RoleName";
            ddlRoles.DataValueField = "ID";
            ddlRoles.DataBind();
        }
        #region Page methods

        private void GetDataByUserID()
        {
            if (Request.QueryString["UserID"] != null && Request.QueryString["UserID"] != "")
            {
                BLL.Login objLogin = new BLL.Login();
                DataTable dt = objLogin.SelectByID(Convert.ToInt32(Request.QueryString["UserID"]));
                if (dt.Rows.Count > 0)
                {
                    txtUserName.Text = dt.Rows[0]["LoginName"].ToString();
                    txtPassword.Text = dt.Rows[0]["LoginPassword"].ToString();
                    txtEmployeeName.Text = dt.Rows[0]["ContactName"].ToString();
                    txtConfrimPassword.Text = dt.Rows[0]["LoginPassword"].ToString();
                    txtEmailID.Text = dt.Rows[0]["EmailID"].ToString();
                    ddlRoles.SelectedValue = dt.Rows[0]["RoleID"].ToString();
                    ddlStatus.SelectedValue = Convert.ToBoolean(dt.Rows[0]["IsActive"]) == true ? "2" : "1";
                    hdnID.Value = dt.Rows[0]["ID"].ToString();
                }
                else
                {
                    ClearFields();
                }
            }
        }
        #endregion

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("ManageUsers.aspx" + BLL.Common.GetQueryStringValues());
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            divAlert.Visible = false;
            BO.Users BO_objLogin = new BO.Users();
            BO_objLogin.ID = Convert.ToInt32(hdnID.Value.Trim());
            BO_objLogin.LoginName = txtUserName.Text.Trim();
            BO_objLogin.EmailID = txtEmailID.Text.Trim();
            BO_objLogin.LoginPassword = txtPassword.Text.Trim();
            BO_objLogin.IsActive = Convert.ToBoolean(ddlStatus.SelectedValue=="2"?1:0);
            BO_objLogin.LastUpdatedBy = Convert.ToInt32(Session["UserID"].ToString());
            BO_objLogin.RoleID = Convert.ToInt32(ddlRoles.SelectedValue);
            BO_objLogin.ContactName = txtEmployeeName.Text.Trim();


            BLL.Login BLL_objLogin = new BLL.Login();
            BLL_objLogin.Save(BO_objLogin);
            divAlert.Visible = true;
            if (BO_objLogin.ID == 0)
            {
                //ltrMessage.Text = "This record has been saved successfully.";
            }
            else
            {
                //ltrMessage.Text = "This record has been updated successfully.";
            }
            Response.Redirect("ManageUsers.aspx" + BLL.Common.GetQueryStringValues());
            //ClearFields();
        }
        private void ClearFields()
        {
            hdnID.Value = "0";

        }
    }
}